<?php
namespace App\Service\Item\Fxotary\Front;

use App\Service\Item\Fxotary\BaseService as Service;
use Illuminate\Support\Facades\DB;

class DetailService extends Service
{
    //内容

    /**
     * 首页
     */
    public function getHomeList()
    {
        return $this->common(7);
    }

    /**
     * 首页2
     */
    public function getHome2List()
    {
        return $this->common(8);
    }

    /**
     * 关于我们
     */
    public function getAbout()
    {
        return $this->common(9);
    }

    /**
     * 服务列表
     */
    public function getServiceList()
    {
        return $this->common(10);
    }

    /**
     * 服务详情
     */
    public function getServiceDetail()
    {
        return $this->common(11);
    }

    /**
     * 投资列表
     */
    public function getPortfolioList()
    {
        return $this->common(12);
    }

    /**
     * 投资网格
     */
    public function getPortfolioGrid()
    {
        return $this->common(13);
    }

    /**
     * 投资详情
     */
    public function getPortfolioDetail()
    {
        return $this->common(14);
    }

    /**
     * 团队
     */
    public function getTeam()
    {
        return $this->common(15);
    }

    /**
     * 博客网格
     */
    public function getBlogGrid()
    {
        return $this->common(16);
    }

    /**
     * 博客列表
     */
    public function getBlogList()
    {
        return $this->common(17);
    }

    /**
     * 博客详情
     */
    public function getBlogDetail()
    {
        return $this->common(18);
    }

    /**
     * 类型我们
     */
    public function getContact()
    {
        return $this->common(19);
    }

    /**
     * 公共的
     */
    public function common($mid)
    {
        $db = $this->dbConn($this->xmData)->where('disabled', 0)->where('menu_id', $mid);
        $res = $db->select('id', 'field_en as fkey', 'type', 'field_type as ftype', 'field_val as fval')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有数据', []);
        }
        foreach ($res as $k => $v) {
            if ($v->ftype == 4) {
                $fvals = explode("\n", $v->fval);
                foreach ($fvals as $kk => $vv) {
                    $fvals[$kk] = explode('||||', $vv);
                }
                $res[$k]->fval = $fvals;
                $res[$k]->domain = env('DOMAIN');
            }
        };
        return $this->toback(0, '', $res);
    }

    /**
     * 保存用户
     */
    public function addUser($pas)
    {
        $res = (array) $this->dbConn($this->xmUser)
            ->where('nick', $pas['nick'])
            ->where('phone', $pas['phone'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '用户已存在');
        }
        $uid = $this->dbConn($this->xmUser)->insertGetId([
            'nick' => $pas['nick'], 'phone' => $pas['phone'],
            'passwd' => md5('123456'), 'addtime' => toDate(),
            'json' => json_encode($pas, JSON_UNESCAPED_UNICODE),
        ]);
        if (!$uid) {
            return $this->toback(-2, '添加失败');
        }
        return $this->toback();
    }
}